#include <iostream>
#include "sylar/http/http_connection.h"
#include "sylar/log.h"
#include "sylar/iomanager.h"


static sylar::Logger::ptr g_logger = SYLAR_LOG_ROOT();

void run(){
	sylar::Address::ptr addr = sylar::Address::LookupAnyIPAddress("www.sylar.top:80");
	if(!addr){
		SYLAR_LOG_ERROR(g_logger) << "get addr error";
		return ;
	}
	SYLAR_LOG_INFO(g_logger) << *addr;
	sylar::Socket::ptr sock = sylar::Socket::CreateTCP(addr);
	bool rt = sock->connect(addr);
	if(!rt){
		SYLAR_LOG_ERROR(g_logger) << "connect " << *addr << " error";
		return ;
	}

	sylar::http::HttpConnection::ptr conn(new sylar::http::HttpConnection(sock));
	sylar::http::HttpRequest::ptr req(new sylar::http::HttpRequest);

	req->setPath("/blog/");
	req->setHeader("host", "www.sylar.top");
	SYLAR_LOG_INFO(g_logger) << "req:" << std::endl << *req;

	conn->sendRequest(req);
	auto rsp = conn->recvResponse();

	if(!rsp){
		SYLAR_LOG_INFO(g_logger) << "recv respone error";
		return ;
	}
	SYLAR_LOG_INFO(g_logger) << "rsp" << std::endl << *rsp;

	SYLAR_LOG_INFO(g_logger) << "-----------------------------------------";
	
	auto ret = sylar::http::HttpConnection::DoGet("http://www.sylar.top/", 300);
	
	SYLAR_LOG_INFO(g_logger) << "result=" << ret->result << " errno=" << errno << " errstr=" << strerror(errno)
		<< " rsp=" << (rsp ? rsp->toString() : "");

}

void test_pool(){
	sylar::http::HttpConnectionPool::ptr pool(new sylar::http::HttpConnectionPool(
		"www.sylar.top", "", 80, 10, 1000 * 30, 20
	));
	sylar::IOManager::GetThis()->addTimer(1000, [pool](){
		auto r = pool->doGet("/", 300);
		SYLAR_LOG_INFO(g_logger) << r->toString();
		return ;
	}, true);
}

int main()
{
	// freopen("./temp.txt", "w", stdout);
	sylar::IOManager iom(2);
	iom.schedule(test_pool);

	return 0;
}